package com.huawei.hms.auth.api.signin;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import com.huawei.hms.auth.api.signin.internal.e;
import com.huawei.hms.auth.api.signin.internal.i;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.common.internal.Preconditions;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.api.hwid.HuaweiIdSignInOptions;
import com.huawei.hms.support.api.hwid.SignInHuaweiId;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import o.fjo;
import o.fjx;

public final class HuaweiIdSignIn {
    public static HuaweiIdSignInClient getClient(Context context, HuaweiIdSignInOptions huaweiIdSignInOptions) {
        return new e(context, huaweiIdSignInOptions);
    }

    public static HuaweiIdSignInClient getClient(Activity activity, HuaweiIdSignInOptions huaweiIdSignInOptions) {
        return new e(activity, huaweiIdSignInOptions);
    }

    public static fjo<SignInHuaweiId> getSignedInAccountFromIntent(Intent intent) {
        fjx o_fjx = new fjx();
        HuaweiIdSignInResult a = i.a(intent);
        if (a == null) {
            o_fjx.ॱ(new ApiException(new Status(8)));
        } else if (!a.isSuccess() || a.getSignInAccount() == null) {
            o_fjx.ॱ(new ApiException(a.getStatus()));
        } else {
            o_fjx.ॱ(a.getSignInAccount());
        }
        return o_fjx.ॱ();
    }

    public static SignInHuaweiId getLastSignedInAccount(Context context) {
        return i.b();
    }

    public static SignInHuaweiId getAccountForScopes(Context context, Scope scope, Scope... scopeArr) {
        Preconditions.checkNotNull(context, "please provide valid Context");
        Preconditions.checkNotNull(scope, "please provide at least one valid scope");
        SignInHuaweiId lastSignedInAccount = getLastSignedInAccount(context);
        if (lastSignedInAccount == null) {
            lastSignedInAccount = SignInHuaweiId.createDefault();
        }
        lastSignedInAccount.requestExtraScopes(new Scope[]{scope});
        lastSignedInAccount.requestExtraScopes(scopeArr);
        return lastSignedInAccount;
    }

    public static SignInHuaweiId getAccountForExtension(Context context, HuaweiIdSignInOptionsExtension huaweiIdSignInOptionsExtension) {
        Preconditions.checkNotNull(context, "please provide valid Context");
        Preconditions.checkNotNull(huaweiIdSignInOptionsExtension, "please provide valid huaweiIdSignInOptionsExtension");
        SignInHuaweiId lastSignedInAccount = getLastSignedInAccount(context);
        if (lastSignedInAccount == null) {
            lastSignedInAccount = SignInHuaweiId.createDefault();
        }
        return lastSignedInAccount.requestExtraScopes(a(huaweiIdSignInOptionsExtension.getImpliedScopes()));
    }

    public static boolean hasPermissions(SignInHuaweiId signInHuaweiId, HuaweiIdSignInOptionsExtension huaweiIdSignInOptionsExtension) {
        Preconditions.checkNotNull(huaweiIdSignInOptionsExtension, "Please provide a non-null HuaweiIdSignInOptionsExtension");
        return hasPermissions(signInHuaweiId, a(huaweiIdSignInOptionsExtension.getImpliedScopes()));
    }

    public static boolean hasPermissions(SignInHuaweiId signInHuaweiId, Scope... scopeArr) {
        if (signInHuaweiId == null) {
            return false;
        }
        if (scopeArr == null) {
            return true;
        }
        Collection hashSet = new HashSet();
        Collections.addAll(hashSet, scopeArr);
        return signInHuaweiId.getGrantedScopes().containsAll(hashSet);
    }

    public static void requestPermissions(Activity activity, int i, SignInHuaweiId signInHuaweiId, HuaweiIdSignInOptionsExtension huaweiIdSignInOptionsExtension) {
        Preconditions.checkNotNull(activity, "Activity should not be null");
        Preconditions.checkNotNull(huaweiIdSignInOptionsExtension, "huaweiIdSignInOptionsExtension should not be null");
        requestPermissions(activity, i, signInHuaweiId, a(huaweiIdSignInOptionsExtension.getImpliedScopes()));
    }

    public static void requestPermissions(Fragment fragment, int i, SignInHuaweiId signInHuaweiId, HuaweiIdSignInOptionsExtension huaweiIdSignInOptionsExtension) {
        Preconditions.checkNotNull(fragment, "Fragment should not be null");
        Preconditions.checkNotNull(huaweiIdSignInOptionsExtension, "huaweiIdSignInOptionsExtension should not be null");
        requestPermissions(fragment, i, signInHuaweiId, a(huaweiIdSignInOptionsExtension.getImpliedScopes()));
    }

    public static void requestPermissions(Activity activity, int i, SignInHuaweiId signInHuaweiId, Scope... scopeArr) {
        Preconditions.checkNotNull(activity, "Activity should not be null");
        Preconditions.checkNotNull(scopeArr, "Scope should not be null");
        activity.startActivityForResult(a(activity, scopeArr), i);
    }

    public static void requestPermissions(Fragment fragment, int i, SignInHuaweiId signInHuaweiId, Scope... scopeArr) {
        Preconditions.checkNotNull(fragment, "Fragment should not be null");
        Preconditions.checkNotNull(scopeArr, "Fragment should not be null");
        fragment.startActivityForResult(a(fragment.getActivity(), scopeArr), i);
    }

    private static Intent a(Activity activity, Scope... scopeArr) {
        return getClient(activity, i.a(scopeArr)).getSignInIntent();
    }

    private static Scope[] a(List<Scope> list) {
        if (list == null) {
            return new Scope[0];
        }
        return (Scope[]) list.toArray(new Scope[list.size()]);
    }
}
